#!/bin/bash 
 
# 自动部署脚本 - CentOS 7+ 环境 
# 作者：AI助手 
# 版本：2.0 
 
# 颜色常量定义 
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m'
 
# 初始化环境 
echo -e "${YELLOW}=== 步骤 0: 环境准备 ===${NC}"
sleep 1 
 
# 更新yum源（替换为阿里云源）
echo -e "${YELLOW}更新yum源...${NC}"
curl -o /etc/yum.repos.d/CentOS-Base.repo  https://mirrors.aliyun.com/repo/CentOS-7.repo  > /dev/null 2>&1 
if [ $? -ne 0 ]; then 
    echo -e "${RED}yum源更新失败，请检查网络${NC}"
    exit 1 
fi 
yum clean all > /dev/null 2>&1 
yum makecache > /dev/null 2>&1 
 
# 关闭防火墙 
echo -e "${YELLOW}关闭防火墙...${NC}"
systemctl stop firewalld && systemctl disable firewalld > /dev/null 2>&1 
if [ $? -ne 0 ]; then 
    echo -e "${RED}防火墙关闭失败，请手动检查${NC}"
    exit 1 
fi 
 
# 安装依赖库 
echo -e "${YELLOW}安装 libz.so.1  依赖...${NC}"
yum install -y zlib > /dev/null 2>&1 
if [ $? -ne 0 ]; then 
    echo -e "${RED}依赖安装失败，请检查网络${NC}"
    exit 1 
fi 
 
# === 软件安装模块 === 
install_software() {
    local pkg_name=$1 
    local install_cmd=$2 
    if rpm -q $pkg_name > /dev/null 2>&1; then 
        echo -e "${YELLOW}$pkg_name 已安装，开始卸载...${NC}"
        yum remove -y $pkg_name > /dev/null 2>&1 
        if [ $? -ne 0 ]; then 
            echo -e "${RED}$pkg_name 卸载失败${NC}"
            exit 1 
        fi 
    fi 
    echo -e "${YELLOW}安装 $pkg_name...${NC}"
    $install_cmd > /dev/null 2>&1 
    if [ $? -ne 0 ]; then 
        echo -e "${RED}$pkg_name 安装失败${NC}"
        exit 1 
    fi 
}
 
# 安装Nginx 1.18 
echo -e "${YELLOW}=== 步骤 2: 安装Nginx 1.18 ===${NC}"
install_software nginx "yum install -y nginx"
 
# 安装MySQL 5.6 
echo -e "${YELLOW}=== 步骤 3: 安装MySQL 5.6 ===${NC}"
# 添加MySQL Yum仓库 
cat <<EOF > /etc/yum.repos.d/mysql56.repo  
[mysql56]
name=MySQL 5.6 Community Server 
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/ 
enabled=1 
gpgcheck=1 
gpgkey=https://repo.mysql.com/RPM-GPG-KEY-mysql-2022  
EOF 
install_software mysql-server "yum install -y mysql-server"
 
# 安装PHP 5.6 
echo -e "${YELLOW}=== 步骤 4: 安装PHP 5.6 ===${NC}"
# 启用Remi仓库 
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm  > /dev/null 2>&1 
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm  > /dev/null 2>&1 
yum-config-manager --enable remi-php56 > /dev/null 2>&1 
install_software php "yum install -y php php-fpm php-mysqlnd"
 
# 启动服务 
systemctl start nginx 
systemctl start mysqld 
systemctl start php-fpm 
 
# === 文件处理模块 === 
# 下载并解压文件 
echo -e "${YELLOW}=== 步骤 5: 解压文件 ===${NC}"
sleep 1 
 
# 自动获取服务器IP 
SERVER_IP=$(ip route get 1.1.1.1 | awk '{print $NF}' | head -1)
echo -e "${YELLOW}检测到服务器IP: ${SERVER_IP}${NC}"
 
# 下载文件（示例URL需替换）
echo -e "${YELLOW}下载 linglong.tar.gz...${NC}" 
curl -L -o /linglong.tar.gz  https://gitee.com/luoranbin/smauto/blob/master/linglong.tar.gz  > /dev/null 2>&1 
if [ $? -ne 0 ]; then 
    echo -e "${RED}文件下载失败，请检查URL${NC}"
    exit 1 
fi 
 
# 解压文件 
echo -e "${YELLOW}解压文件...${NC}"
tar -zxvf /linglong.tar.gz  -C / > /dev/null 2>&1 
if [ $? -ne 0 ]; then 
    echo -e "${RED}解压失败，请检查文件完整性${NC}"
    exit 1 
fi 
 
# 设置权限 
echo -e "${YELLOW}设置目录权限...${NC}"
chmod -R 777 /game /66kj > /dev/null 2>&1 
echo -e "${GREEN}权限设置完成${NC}"
 
# 数据库配置 
echo -e "${YELLOW}=== 步骤 6: 数据库配置 ===${NC}"
sleep 1 
 
# 获取用户密码 
read -s -p "请输入MySQL root密码: " MYSQL_PASS 
echo 
read -s -p "请再次输入密码确认: " MYSQL_PASS_CONFIRM 
if [ "$MYSQL_PASS" != "$MYSQL_PASS_CONFIRM" ]; then 
    echo -e "${RED}密码不一致，请重新运行脚本${NC}"
    exit 1 
fi 
 
# 设置数据库权限 
echo -e "${YELLOW}设置数据库权限...${NC}"
mysql -u root -p"$MYSQL_PASS" -e "
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '$MYSQL_PASS' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '$MYSQL_PASS' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '$MYSQL_PASS' WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost.localdomain'  IDENTIFIED BY '$MYSQL_PASS' WITH GRANT OPTION;
FLUSH PRIVILEGES;
" > /dev/null 2>&1 
if [ $? -ne 0 ]; then 
    echo -e "${RED}数据库权限设置失败${NC}"
    exit 1 
else 
    echo -e "${GREEN}权限设置成功${NC}"
fi 
 
# 导入数据库 
echo -e "${YELLOW}导入数据库...${NC}"
cd /game || exit 
./sk > /dev/null 2>&1 
if [ $? -ne 0 ]; then 
    echo -e "${RED}数据库导入失败，请检查脚本路径${NC}"
    exit 1 
else 
    echo -e "${GREEN}数据库导入完成${NC}"
fi 
 
# IP修改 
echo -e "${YELLOW}=== 步骤 7: 修改IP配置 ===${NC}"
sleep 1 
 
# 获取用户输入IP 
read -p "请输入目标IP（默认$SERVER_IP）: " INPUT_IP 
TARGET_IP=${INPUT_IP:-$SERVER_IP}
 
# 配置文件列表 
CONFIG_FILES=(
    "/game/db/db_server_user.ini" 
    "/game/game/server_user.ini" 
    "/game/login/login_user.ini" 
)
 
# 修改IP 
echo -e "${YELLOW}正在修改配置文件...${NC}"
for FILE in "${CONFIG_FILES[@]}"; do 
    # 使用正则表达式匹配所有IP字段 
    sed -i "s/\(ListenIp\|Server\|ListenPortal\|ConnectIp\|location\)[[:space:]]*=[[:space:]]*\"202\.140\.143\.70\"/\1 = \"$TARGET_IP\"/g" "$FILE" > /dev/null 2>&1 
    if [ $? -ne 0 ]; then 
        echo -e "${RED}修改文件 $FILE 失败${NC}"
        exit 1 
    fi 
done 
echo -e "${GREEN}IP修改完成${NC}"
 
# 最后检查 
echo -e "${YELLOW}=== 部署完成 ===${NC}"
echo -e "${GREEN}访问 http://$TARGET_IP 查看效果${NC}"
echo -e "${GREEN}MySQL root密码：$MYSQL_PASS${NC}"